package com.six.service.impl;

import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.six.mapper.SupplierMapper;
import com.six.mapper.SupplierMaterialMapper;
import com.six.pojo.Supplier;
import com.six.pojo.SupplierMaterial;
import com.six.pojo.dto.SupplierDto;
import com.six.pojo.vo.SupplierVo;
import com.six.service.SupplierService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
@Slf4j
public class SupplierServiceImpl extends ServiceImpl<SupplierMapper, Supplier> implements SupplierService {

    @Autowired(required = false)
    private SupplierMaterialMapper supplierMaterialMapper;
    @Override
    public Page<SupplierDto> findSupplierPageByQueryVo(Page page, SupplierVo supplierVo) {
        return getBaseMapper().findSupplierPageByQueryVo(page,supplierVo);
    }

    @Override
    public SupplierDto findSupplierById(Long id) {
        return getBaseMapper().findSupplierById(id);
    }

    @Override
    public void addSupplier(Supplier supplier) {
        //插入基本信息
        this.save(supplier);
        //给中间表插入数据
        if (!StringUtils.isEmpty(supplier.getMaterialIds())){
            //非空就有原料
            String[] materialIds = supplier.getMaterialIds().split(",");
            for (String mId:materialIds){
                SupplierMaterial supplierMaterial =new SupplierMaterial();
                supplierMaterial.setMaterialId(Long.valueOf(mId));
                supplierMaterial.setSupplierId(supplier.getId());
                //插入关系表
                supplierMaterialMapper.insert(supplierMaterial);
            }
        }
    }
}
